﻿package net.wangit.view.element.component;

import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.AbstractElementWriter;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.tree.Tree;
import net.wangit.view.unit.UnitConfigurationException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class TreeElementWriter extends AbstractElementWriter {

	private static Log log = LogFactory.getLog(TreeElementWriter.class);

	public TreeElementWriter() {
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		Tree tree = (Tree) ec.getObject();
		if (tree != null) {
			tree.getConfig().setVisible(true);
			writeControl(html, tree);
		}
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeDisabledControl(ActionContext actioncontext,
			StringBuffer stringbuffer, ElementComponent elementcomponent,
			Row row1) {
	}

	protected void writeInvisibleControl(StringBuffer html,
			ElementComponent ec, Row row) {
		Tree tree = (Tree) ec.getObject();
		if (tree != null) {
			tree.getConfig().setVisible(false);
			writeControl(html, tree);
		}
	}

	protected void writeHidden(ActionContext actioncontext,
			StringBuffer stringbuffer, ElementComponent elementcomponent,
			Row row1) {
	}

	private void writeControl(StringBuffer html, Tree tree) {
		try {
			html.append(tree.write());
		} catch (Exception e) {
			log.error((new StringBuilder("TreeElement error: ")).append(
					tree.getId()).toString());
			throw new UnitConfigurationException("TreeElement error: ", e);
		}
	}

}
